package cn.com.code.design.factory;

import cn.com.code.design.workshop.ICacheAdapter;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * 代理抽象工厂
 *
 * @author zhengpei
 * @date 2021/9/13
 */
public class JDKProxyFactory {

  public static <T> T getProxy(Class cacheClazz, Class<? extends ICacheAdapter> cacheAdapter) throws IllegalAccessException, InstantiationException {
    InvocationHandler handler = new JDKInvocationHandler(cacheAdapter.newInstance());
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    Object o = Proxy.newProxyInstance(contextClassLoader, new Class[]{cacheClazz}, handler);
    return (T) o;
  }

}
